package mbp.gui.panels;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import mbp.core.Offer;
import mbp.gui.base.IListPanel;
import mbp.message.offer;

public class OfferListPanel extends JPanel  implements IListPanel
{
	private JTable ofrTable;
	private DefaultTableModel ofrModel;
	
	private List<offer> ofriList;
	private List<Offer> ofrList;
	
	private JScrollPane scrollPane;
	
	private String agentName = "";

	/**
	 * Create the panel.
	 */
	public OfferListPanel() 
	{
		setLayout(new BorderLayout(0, 0));
		scrollPane = new JScrollPane();
		add(scrollPane);
		
		ofrTable = new JTable(){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		  };
		  
		  ofrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		  ofrTable.setFillsViewportHeight(true);
		  scrollPane.setViewportView(ofrTable);
			
		  ofrTable.setModel(new DefaultTableModel(
					new Object[][] {
					},
					new String[] {
							"Valid date", "Service type", "Server", "UUID"
					}
				));
		  ofrModel = (DefaultTableModel) ofrTable.getModel();
		  
		  //podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		  ofrTable.addMouseListener(new MouseAdapter(){
			  public void mouseClicked(MouseEvent e){
				  if (e.getClickCount() == 2){
					  processDoubleClick(ofrTable.getSelectedRow());
				  }
			  }
		  } );
		  
		  //ofrTable.getColumnModel().getColumn(0).setMinWidth(50);
		  //ofrTable.getColumnModel().getColumn(0).setMinWidth(50);

	}
	
	public void processDoubleClick(int index)
	{
		System.out.println(" double click: selected row: " + ofrTable.getSelectedRow());
	}
	
	private void reloadInfo()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				ofrModel.setRowCount(0);
				
				for(int i=0; i<ofriList.size(); i++)
				{
					//RcModel.setRowCount(i);
					String validity = ofriList.get(i).offerValidity;
					String type = ofriList.get(i).type;
					String server = ofriList.get(i).server;
					String uuid = ofriList.get(i).uuid;
					ofrModel.insertRow(i, new Object[]{validity, type, server, uuid});
				}
				
			}
		});
	}
	
	private void reloadObj()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				ofrModel.insertRow(0, new Object[]{"Not implemented", "Not implemented", "Not implemented", "Not implemented"});				
			}
		});
	}

	@Override
	public <offer> void reloadListInfo(List<offer> infoList) {
		this.ofriList = (List<mbp.message.offer>) infoList;
		this.ofrList = null;
		reloadInfo();		
	}

	@Override
	public <Offer> void reloadListObj(List<Offer> objList) {
		this.ofrList = (List<mbp.core.Offer>) objList;
		this.ofriList = null;
		reloadObj();		
	}

	@Override
	public void reloadList() {
		// TODO Auto-generated method stub
		
	}

}
